import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';

import '../config/config.dart';
import '../generated/assets.dart';

class CommonWidget {

  ///
  static commonAppBar(String title, {Widget? rightWidget, Function? callback, Color? bg}) {
    return Container(
        width: Get.width,
        color: bg ?? Config.colorBg2,
        padding: EdgeInsets.symmetric(horizontal: Config.spaceNormal.w),
        child: Container(
          margin: EdgeInsets.only(top: Get.mediaQuery.padding.top),
          height: 100.w,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              GestureDetector(
                onTap: () {
                  Get.back();
                },
                child: Container(
                  width: 120.w,
                  alignment: Alignment.centerLeft,
                  child: Icon(Icons.arrow_back_rounded, color: Config.colorText1, size: 50.w),
                ),
              ),
              Expanded(
                child: Text(
                  title,
                  style: TextStyle(color: Config.colorText1, fontSize: 36.sp,fontWeight: FontWeight.bold),
                  textAlign: TextAlign.center,
                  softWrap: false,
                ),
              ),
              Container(
                width: 120.w,
                alignment: Alignment.centerRight,
                child: rightWidget != null
                    ? GestureDetector(
                        onTap: () {
                          if (callback != null) {
                            callback();
                          }
                        },
                        child: rightWidget,
                      )
                    : SizedBox(
                        width: 100.w,
                      ),
              ),
            ],
          ),
        ));
  }

  ///
  /// 通用的输入框样式
  ///
  static commonEditText(
    bool obscureText,
    Function(String) onChange,
    Function() onTap,
    Function()? rightIconOnTap,
    FocusNode focusNode, {
    IconData? icon,
    TextInputType keyboardType = TextInputType.text,
    int maxLength = 18,
    int maxLines = 1,
    double fontSize = 16,
    Color textColor = Config.colorText1,
    Color hintColor = Config.colorText3,
    String hint = '',
    double radius = 10,
    Color sideColorP = Config.colorMain,
    Color sideColorN = Config.colorBg2,
    Color fillColor = Config.colorBg2,
    bool filled = true,
    double contentPaddingV = 20,
    bool isRightIcon = false,
    String rightText = "",
    double rightIconRight = 10,
    double rightIconSize = 20,
    bool enabled = true,
    TextEditingController? controller,
    Function()? rightTextOnTap,
  }) {
    return Stack(
      children: [
        SizedBox(
          width: double.infinity,
          child: TextField(
            keyboardType: keyboardType,
            textAlign: TextAlign.justify,
            obscureText: obscureText,
            focusNode: focusNode,
            maxLength: maxLength,
            maxLines: maxLines,
            cursorColor: Config.colorMain,
            inputFormatters: [
              // FilteringTextInputFormatter.allow(RegExp("[A-Za-z0-9.@#-_]")),
              LengthLimitingTextInputFormatter(maxLength)
            ],
            onChanged: onChange,
            onTap: onTap,
            enabled: enabled,
            controller: controller,
            style: TextStyle(fontSize: fontSize, color: textColor),
            decoration: InputDecoration(
              hintText: hint,
              counterText: '',
              hintStyle: TextStyle(color: hintColor),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(radius),
                ),
                borderSide: BorderSide(color: sideColorP, width: 2.5.w),
              ),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(radius),
                ),
                borderSide: BorderSide(color: sideColorN, width: 2.5.w),
              ),
              disabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(radius),
                ),
                borderSide: BorderSide(color: sideColorN, width: 2.5.w),
              ),
              fillColor: fillColor,
              filled: filled,
              prefixIcon: icon != null
                  ? Icon(
                      icon,
                      color: textColor,
                      size: rightIconSize,
                    )
                  : null,
              contentPadding: EdgeInsets.symmetric(
                  vertical: contentPaddingV, horizontal: icon != null ? 0 : 32.w),
            ),
          ),
        ),
        Visibility(
          visible: isRightIcon == true,
          child: Positioned(
            right: rightIconRight,
            top: 0,
            bottom: 0,
            child: InkWell(
              onTap: rightIconOnTap,
              child: Icon(
                obscureText ? Icons.visibility_off_outlined : Icons.visibility_outlined,
                size: rightIconSize,
                color: textColor,
              ),
            ),
          ),
        ),
        Visibility(
          visible: rightText.isNotEmpty,
          child: Positioned(
            right: rightIconRight,
            top: 0,
            bottom: 0,
            child: InkWell(
              onTap: rightTextOnTap,
              child: Row(
                children: [
                  Text(
                    rightText,
                    textAlign: TextAlign.center,
                    style: TextStyle(color: Config.colorText1, fontSize: Config.textSizeContent.w),
                  ),
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }

  ///
  /// 通用的按钮
  /// TextButton
  /// or
  /// ElevatedButton
  ///
  static commonButton(
    String text,
    Function() onTap, {
    Color bgColor = Config.colorMain,
    Color fgColor = Config.colorText1,
    double fontSize = 15,
    double radius = 10,
    double sideWidth = 1.5,
    Color sideColor = Config.colorText1,
    double paddingH = 0,
    double width = 150,
  }) {
    return SizedBox(
      width: width,
      height: 98.w,
      child: ElevatedButton(
        onPressed: onTap,
        style: ButtonStyle(
            backgroundColor: WidgetStateProperty.all(bgColor),
            foregroundColor: WidgetStateProperty.all(fgColor),
            textStyle: WidgetStateProperty.all(TextStyle(fontSize: fontSize)),
            shape: WidgetStateProperty.all(
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(radius))),
            side: WidgetStateProperty.all(BorderSide(width: sideWidth, color: sideColor)),
            padding: WidgetStateProperty.all(EdgeInsets.symmetric(horizontal: paddingH))),
        child: Text(text),
      ),
    );
  }

  ///
  /// 暂无数据
  ///
  static showNullData({String tip = ''}) {
    return SizedBox(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          SvgPicture.asset(
            Assets.iconsIcNullData,
            width: 150.w,
            height: 150.w,
          ),
          Text(
            tip.isEmpty ? 'No data' : tip,
            style: TextStyle(color: Config.colorText3, height: 1.8, fontSize: 26.w),
          ),
          SizedBox(
            height: 80.w,
          ),
        ],
      ),
    );
  }

  ///
  /// 分割线
  ///
  static commonDivider() {
    return Divider(thickness: 1.6.w, height: 1.7.w, color: Config.colorText3.withOpacity(0.1));
  }
}
